#include <QtGui>
#include "calbform.h"
//#include "../core/aprcfg.h"
#include "../core/measprog.h"
#include "../core/ctrl.h"

CalbForm::CalbForm(QWidget* parent)
	: QMdiSubWindow(parent)
{
//// 通用控件初始化 ////////////////////////////////////////////////////////////

	lblCalbName = new QLabel;	// 文字内容由cmbItemSelc改变时决定
	cmbItemSelc = new QComboBox;
	btnStartCalb = new QPushButton(tr("START"));
	btnManCalb	 = new QPushButton(tr("MANUAL"));
	btnManCalb->setVisible(false);	// 当是vl测试,且cooling2为0时才需要	
	btnSaveCalb = new QPushButton(tr("SAVE"));
	btnSaveCalb->setEnabled(false);	// 至少完成一次master测试后尚可保持

	// dlm需要14, vl需要11行 (?)
	table = new QTableWidget(14, 1); 
	table->horizontalHeader()->setVisible(false);

	QLabel* label;
	for(int rowIndex = 0; rowIndex < 14; ++rowIndex){
		label = new QLabel;
		label->setAlignment(Qt::AlignCenter);
		table->setCellWidget(rowIndex, 0, label);
	}
	
//	QStringList headers;
//	headers << tr("TEMP(deg)") << tr("VALUE1") << tr("VALUE2") 
//			<< tr("V2/V1") << tr("C_LOG") << tr("G_NUM") << tr("L_NUM")
//			<< tr("RANGE1") << tr("GAIN1") << tr("RANGE2") << tr("GAIN2");
//	table->setVerticalHeaderLabels(headers);

///// 布局 /////////////////////////////////////////////////////////////////////

	//
	// 窗口上部
	//
	QHBoxLayout* topLayout = new QHBoxLayout;
	topLayout->addWidget(lblCalbName);
	topLayout->addWidget(cmbItemSelc);
	topLayout->addWidget(btnStartCalb);
	topLayout->addWidget(btnManCalb);
	topLayout->addWidget(btnSaveCalb);

	//
	// 下部
	//
	QVBoxLayout* btmLeftLayout = new QVBoxLayout;
	hueFrame = initHueCtrlsLayout();
	dlmFrame = initDlmCtrlsLayout();
	vlFrame  = initVlCtrlsLayout();
	btmLeftLayout->addWidget(hueFrame);
	btmLeftLayout->addWidget(dlmFrame);
	btmLeftLayout->addWidget(vlFrame);
	btmLeftLayout->addStretch();  	

	QHBoxLayout* btmLayout = new QHBoxLayout;
//	btmLayout->addWidget(hueFrame);
	btmLayout->addLayout(btmLeftLayout);
	btmLayout->addWidget(table);

	//
	// 整体
	//
	QVBoxLayout* layout = new QVBoxLayout;
	layout->addLayout(topLayout);
	layout->addLayout(btmLayout);

	QFrame* frame = new QFrame;
	frame->setLayout(layout);

	setWidget(frame);

	setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
	showMaximized();

///// 读入设置 /////////////////////////////////////////////////////////////////

///// 联接SIGNAL和SLOT /////////////////////////////////////////////////////////

	connect(cmbItemSelc, SIGNAL(currentIndexChanged(const QString&)),
				this, SLOT(changeItemType(const QString&)));
	connect(cmbRefVlRange, SIGNAL(currentIndexChanged(const QString&)),
				this, SLOT(changeVlRange(const QString&)));
	connect(cmbRefBiasRange, SIGNAL(currentIndexChanged(const QString&)),
				this, SLOT(changeBiasRange(const QString&)));
	connect(btnStartCalb, SIGNAL(clicked()), this, SLOT(startCalb()));
	connect(btnSaveCalb, SIGNAL(clicked()), this, SLOT(saveCalbToProg()));

}

//
// 将hue_cfg等相关设置保存到program中
//
void CalbForm::saveCalbSetting()
{
	MeasProg* pMeasProg = MeasProg::getInstance();
	program_s* p = pMeasProg->getProgPtr();

	//kind_s* pk = &p->kinds[0];
	p->itemno = itemno;

	//hue_cfg* hue;
	kind_s* kp = &p->kinds[0];

	if(0 == calb_no){
		// hue
		p->meastype = TYPE_HUEMASTER;
		
		ushort ofstCode = 0;
		AprCfg::getParamCodeByName(cmbOfsCancel->currentText(), PM_ONOFF_ATTR, ofstCode);
		kp->ofstcancel = ofstCode;

		kp->cooldown = spinCooling->value();

		kp->hue_mlums[chn_no-1] = dspinRefWavel->value();	// NOTE!:不知是否正确

	//	hue = &p->kinds[0].hue_cfg[chn_no];
	//	hue->active = PM_ON;
	//	hue->Mastertemp = 250;

	}else if(1 == calb_no){
		// vl
		p->meastype = TYPE_VLMASTER1; // vl2 master另行处理
		
		ushort brngCode = 0;
		AprCfg::getParamCodeByName(cmbRefBiasRange->currentText(), PM_BRANGE_ATTR, brngCode);	// NOTE!:可能会出错!!
		kp->vl_cfg[chn_no-1].brng = brngCode;
	
		ushort mrngCode = 0;
		AprCfg::getParamCodeByName(cmbRefVlRange->currentText(), PM_MRANGE_ATTR, mrngCode);
		kp->vl_cfg[chn_no-1].mrng = mrngCode;

		ushort filterCode = 0;
		AprCfg::getParamCodeByName(cmbFilter->currentText(), PM_FIL_ATTR, filterCode);
		kp->vl_cfg[chn_no-1].filter = filterCode;

		kp->vl_cfg[chn_no-1].waittime = dspinWaitTime->value();

		kp->vl_cfg[chn_no-1].vlimit = dspinVLimit->value();

		kp->vl_cfg[chn_no-1].b_v1 = dspinRefBias1->value();
		kp->vl_cfg[chn_no-1].b_v2 = dspinRefBias2->value();
		kp->vl_cfg[chn_no-1].t_v1 = dspinRefVl1->value();
		kp->vl_cfg[chn_no-1].t_v2 = dspinRefVl2->value();

		kp->cool_v1 = spinCoolingVl->value();
		kp->cool_v2 = spinCooling2->value();

		ushort vlOfsCode = 0;
		AprCfg::getParamCodeByName(cmbOfsCancelVl->currentText(), PM_ONOFF_ATTR, vlOfsCode);
		kp->ofstcancel = vlOfsCode;

	}else if(2 == calb_no){
		// dlm
		p->meastype = TYPE_DLMASTER;

		kp->dlm_cfg[chn_no-1].x0 = dspinDlmX->value();
		kp->dlm_cfg[chn_no-1].y0 = dspinDlmY->value();
		
		kp->cooldown = spinCoolingDlm->value();

		ushort dlmOfsCode = 0;
		AprCfg::getParamCodeByName(cmbOfsCancelDlm->currentText(), PM_ONOFF_ATTR, dlmOfsCode);
		kp->ofstcancel = dlmOfsCode;
		
	}else{
		// should never happen
	}
}

//
// hue相关控件的初始化&布局
//
QFrame* CalbForm::initHueCtrlsLayout()
//QGridLayout* CalbForm::initHueCtrlsLayout()
{
	QLabel* lblRefWavel = new QLabel(tr("REF.WAVEL (nm):"));
	QLabel* lblRefTemp	= new QLabel(tr("REF.TEMP (deg):"));
	QLabel* lblFilter	= new QLabel(tr("FILTER:"));
	QLabel* lblOfsCancel= new QLabel(tr("OFS.CANCEL:"));
	QLabel* lblCooling	= new QLabel(tr("COOLING (ms):"));

	double upper = 0.0;
	double lower = 0.0;
	double step  = 0.0;
	ushort decimals = 0;
//	long coef = 0;
//	QString unit;

	dspinRefWavel = new QDoubleSpinBox;
	AprCfg::getRangeDetailByCode(PM_M770nm, upper, lower, step, decimals);
//	AprCfg::getRangeInfoByCode(PM_M770nm, unit, coef); // 除了770nm,还有另一个档位,什么情况下用??? 
	dspinRefWavel->setRange(lower, upper);
	dspinRefWavel->setSingleStep(step);
	dspinRefWavel->setDecimals(decimals);
//	dspinRefWavel->setSuffix(unit);

	lblTempData = new QLabel(tr("25.0"));
	lblFilterData = new QLabel;	// 应该读取当前项目的filter设定

	cmbOfsCancel = new QComboBox;
	QStringList onOffList;
	AprCfg::getNamesByAttr(PM_ONOFF_ATTR, onOffList);
	cmbOfsCancel->addItems(onOffList);

	spinCooling = new QSpinBox;
	AprCfg::getRangeDetailByCode(PM_COOLT, upper, lower, step, decimals);
//	AprCfg::getRangeInfoByCode(PM_COOLT, unit, coef); 
	spinCooling->setRange(lower, upper);
	spinCooling->setSingleStep(step);
//	spinCooling->setSuffix(unit);

	QGridLayout* glayout = new QGridLayout;
	glayout->addWidget(lblRefWavel, 0, 0);
	glayout->addWidget(dspinRefWavel, 0, 1);
	glayout->addWidget(lblRefTemp, 1, 0);
	glayout->addWidget(lblTempData, 1, 1);
	glayout->addWidget(lblFilter, 2, 0);
	glayout->addWidget(lblFilterData, 2, 1);
	glayout->addWidget(lblOfsCancel, 3, 0);
	glayout->addWidget(cmbOfsCancel, 3, 1);
	glayout->addWidget(lblCooling, 4, 0);
	glayout->addWidget(spinCooling, 4, 1);

//	QVBoxLayout* vlayout = new QVBoxLayout;
//	vlayout->addLayout(glayout);
//	vlayout->addStretch();
	
	QFrame* frame = new QFrame;
//	frame->setLayout(vlayout);
	frame->setLayout(glayout);

	return frame;
}



QFrame* CalbForm::initDlmCtrlsLayout()
{
	QLabel* lblDlmX = new QLabel(tr("X0:"));
	QLabel* lblDlmY = new QLabel(tr("Y0:"));
	QLabel* lblOfsCancel= new QLabel(tr("OFS.CANCEL:"));
	QLabel* lblCooling	= new QLabel(tr("COOLING (ms):"));

	double upper = 0.0;
	double lower = 0.0;
	double step  = 0.0;
	ushort decimals = 0;	
	AprCfg::getRangeDetailByCode(PM_DLMXY, upper, lower, step, decimals);

	dspinDlmX = new QDoubleSpinBox;
	dspinDlmX->setRange(lower, upper);
	dspinDlmX->setSingleStep(step);
	dspinDlmX->setDecimals(decimals);

	dspinDlmY = new QDoubleSpinBox;
	dspinDlmY->setRange(lower, upper);
	dspinDlmY->setSingleStep(step);
	dspinDlmY->setDecimals(decimals);

	cmbOfsCancelDlm = new QComboBox;
	QStringList ofsCancelList;
	AprCfg::getNamesByAttr(PM_ONOFF_ATTR, ofsCancelList);
	cmbOfsCancelDlm->addItems(ofsCancelList);

	spinCoolingDlm = new QSpinBox;
	AprCfg::getRangeDetailByCode(PM_COOLT, upper, lower, step, decimals);
	spinCoolingDlm->setRange(lower, upper);
	spinCoolingDlm->setSingleStep(step);

	QGridLayout* glayout = new QGridLayout;
	glayout->addWidget(lblDlmX, 0, 0);
	glayout->addWidget(dspinDlmX, 0, 1);
	glayout->addWidget(lblDlmY, 1, 0);
	glayout->addWidget(dspinDlmY, 1, 1);
	glayout->addWidget(lblOfsCancel, 2, 0);
	glayout->addWidget(cmbOfsCancelDlm, 2, 1);
	glayout->addWidget(lblCooling, 3, 0);
	glayout->addWidget(spinCoolingDlm, 3, 1);

	QFrame* frame = new QFrame;
	frame->setLayout(glayout);

	return frame;
}

QFrame* CalbForm::initVlCtrlsLayout()
{
	QLabel* lblRefVl1 = new QLabel(tr("REF.VL1:"));
	QLabel* lblRefVl2 = new QLabel(tr("REF.VL2:"));
//	QLabel* lblRefVlRange = new QLabel(tr("REF.VL RANGE:"));
	QLabel* lblRefBias1 = new QLabel(tr("REF.BIAS1:"));
	QLabel* lblRefBias2 = new QLabel(tr("REF.BIAS2:"));
	QLabel* lblWaitTime = new QLabel(tr("WAIT TIME:"));
	QLabel* lblFilter	= new QLabel(tr("FIL.COUNT:"));
	QLabel* lblVLimit	= new QLabel(tr("VOL.LIMIT"));
	QLabel* lblOfsCancel= new QLabel(tr("OFS.CANCEL"));
	QLabel* lblCooling	= new QLabel(tr("COOLING (ms):"));
	QLabel* lblCooling2 = new QLabel(tr("COLLING1-2 (ms):"));

	cmbRefVlRange = new QComboBox;
	QStringList vlRanges;
	// TODO: Well, this is hardcoded for now!
	vlRanges << tr("9.99mcd") << tr("99.9mcd") << tr("999mcd");
	cmbRefVlRange->addItems(vlRanges);

	double upper = 0.0;
	double lower = 0.0;
	double step  = 0.0;
	ushort decimals = 0;

	dspinRefVl1 = new QDoubleSpinBox;
	AprCfg::getRangeDetailByName(cmbRefVlRange->currentText(), 
					PM_MRANGE_ATTR, upper, lower, step, decimals);
	dspinRefVl1->setRange(lower, upper);
	dspinRefVl1->setSingleStep(step);
	dspinRefVl1->setDecimals(decimals);

	dspinRefVl2 = new QDoubleSpinBox;
	dspinRefVl2->setRange(lower, upper);
	dspinRefVl2->setSingleStep(step);
	dspinRefVl2->setDecimals(decimals);

	cmbRefBiasRange = new QComboBox;
	QStringList biasRanges;
	AprCfg::getNamesByAttr(PM_BRANGE_ATTR, biasRanges);
	cmbRefBiasRange->addItems(biasRanges);
	
	dspinRefBias1 = new QDoubleSpinBox;
	AprCfg::getRangeDetailByName(cmbRefBiasRange->currentText(), 
						PM_BRANGE_ATTR, upper, lower, step, decimals);
	dspinRefBias1->setRange(lower, upper);
	dspinRefBias1->setSingleStep(step);
	dspinRefBias1->setDecimals(decimals);	
	
	dspinRefBias2 = new QDoubleSpinBox;
	dspinRefBias2->setRange(lower, upper);
	dspinRefBias2->setSingleStep(step);
	dspinRefBias2->setDecimals(decimals);	

	dspinWaitTime = new QDoubleSpinBox;
	AprCfg::getRangeDetailByCode(PM_T9999ms, upper, lower, step, decimals);
	dspinWaitTime->setRange(lower, upper);
	dspinWaitTime->setSingleStep(step);
	dspinWaitTime->setDecimals(decimals);

	cmbFilter = new QComboBox;
	QStringList filterOptions;
	AprCfg::getNamesByAttr(PM_FIL_ATTR, filterOptions);
	cmbFilter->addItems(filterOptions);

	dspinVLimit = new QDoubleSpinBox;
	AprCfg::getRangeDetailByCode(PM_L9_99V, upper, lower, step, decimals);
	dspinVLimit->setRange(lower, upper);
	dspinVLimit->setSingleStep(step);
	dspinVLimit->setDecimals(decimals);	

	cmbOfsCancelVl = new QComboBox;
	QStringList onOffList;
	AprCfg::getNamesByAttr(PM_ONOFF_ATTR, onOffList);
	cmbOfsCancelVl->addItems(onOffList);

	spinCoolingVl = new QSpinBox;
	AprCfg::getRangeDetailByCode(PM_COOLT, upper, lower, step, decimals);
	spinCoolingVl->setRange(lower, upper);
	spinCoolingVl->setSingleStep(step);
//	spinCoolingVl->setDecimals(decimals);

	spinCooling2 = new QSpinBox;
	spinCooling2->setRange(lower, upper);
	spinCooling2->setSingleStep(step);
//	spinCooling2->setDecimals(decimals);

	QGridLayout* glayout = new QGridLayout;
	glayout->addWidget(lblRefVl1, 0, 0);
	glayout->addWidget(dspinRefVl1, 0, 1);
	glayout->addWidget(cmbRefVlRange, 0, 2);
	glayout->addWidget(lblRefVl2, 1, 0);
	glayout->addWidget(dspinRefVl2, 1, 1);
	glayout->addWidget(lblRefBias1, 2, 0);
	glayout->addWidget(dspinRefBias1, 2, 1);
	glayout->addWidget(cmbRefBiasRange, 2, 2);
	glayout->addWidget(lblRefBias2, 3, 0);
	glayout->addWidget(dspinRefBias2, 3, 1);
	glayout->addWidget(lblWaitTime, 4, 0);
	glayout->addWidget(dspinWaitTime, 4, 1);
	glayout->addWidget(lblFilter, 5, 0);
	glayout->addWidget(cmbFilter, 5, 1);
	glayout->addWidget(lblVLimit, 6, 0);
	glayout->addWidget(dspinVLimit, 6, 1);
	glayout->addWidget(lblOfsCancel, 7, 0);
	glayout->addWidget(cmbOfsCancelVl, 7, 1);
	glayout->addWidget(lblCooling, 8, 0);
	glayout->addWidget(spinCoolingVl, 8, 1);
	glayout->addWidget(lblCooling2, 9, 0);
	glayout->addWidget(spinCooling2, 9 , 1);

	QFrame* frame = new QFrame;
	frame->setLayout(glayout);

	return frame;
}

bool CalbForm::getHueList() 
{
	MeasProg* pMeasProg = MeasProg::getInstance();
	program_s* p = pMeasProg->getProgPtr();

	usechn_t usechn;
	chnitem_t chnitem;
	// 设定item项comboxbox的list
	QStringList items;
	if(AprCfg::getHueItemsList(*p, items, citem, usechn, chnitem) == false){
		return false;
	}
	cmbItemSelc->clear();
	cmbItemSelc->addItems(items);
	return true;
}


// SLOTS /////////////////////////////////////////////////////////////////

void CalbForm::startCalb()  // slot
{	
	// 保存
	saveCalbSetting();
	
	Ctrl* p = Ctrl::getInstance();
	p->cpyProgramToShm();
//	p->setMeasState(true);
	p->startCtrlThread();
}

//
// itemSelc选择的item发生变化时,改变相关显示
//
void CalbForm::changeItemType(const QString& text)
{
	const char * calb_List[3] = {"CAL/HUE", "CAL/VL ", "CAL/DLM"};
//	int calb_no = 0;
	
	if(text.contains(tr("HUE")) || text.contains(tr("POLH"))){
		calb_no = 0;	
	}else if(text.contains(tr("VL"))){
		calb_no = 1;
	}else if(text.contains(tr("DLM"))){
		calb_no = 2;
	}else{
		// should never happen
		return;
	}
	switchResultTable(calb_no);

	char chn = text.at(2).toAscii();
	if('1' == chn){
		chn_no = 1;
	}else if('2' == chn){
		chn_no = 2;
	}else if('3' == chn){
		chn_no = 3;
	}else{
		// should never happen
		return;
	}
//	qDebug() << "!!!!!!!!!!!!!!!Channel No.=" << chn_no;

	QFrame* frames[3] = { hueFrame, vlFrame, dlmFrame };
	for(int i = 0; i < 3; ++i){
		frames[i]->setVisible(i == calb_no);
	}

	lblCalbName->setText(calb_List[calb_no]);


	//
	// 指定program_s中的itemno 
	// NOTE!:待验证
	//
	int index = cmbItemSelc->currentIndex();
	itemno = citem[index];	 
}

void CalbForm::changeVlRange(const QString& text)
{
	double upper = 0.0;
	double lower = 0.0;
	double step  = 0.0;
	ushort decimals = 0;

	AprCfg::getRangeDetailByName(text, 
				PM_MRANGE_ATTR, upper, lower, step, decimals);
	dspinRefVl1->setRange(lower, upper);
	dspinRefVl1->setSingleStep(step);
	dspinRefVl1->setDecimals(decimals);	

	dspinRefVl2->setRange(lower, upper);
	dspinRefVl2->setSingleStep(step);
	dspinRefVl2->setDecimals(decimals);	

}

void CalbForm::changeBiasRange(const QString& text)
{
	double upper = 0.0;
	double lower = 0.0;
	double step  = 0.0;
	ushort decimals = 0;
	
	AprCfg::getRangeDetailByName(text, 
				PM_BRANGE_ATTR, upper, lower, step, decimals);
	dspinRefBias1->setRange(lower, upper);
	dspinRefBias1->setSingleStep(step);
	dspinRefBias1->setDecimals(decimals);

	dspinRefBias2->setRange(lower, upper);
	dspinRefBias2->setSingleStep(step);
	dspinRefBias2->setDecimals(decimals);
}

//
// 改变结果table显示
// type - 0: hue, 1: vl; 2: dlm
//
void CalbForm::switchResultTable(int type)
{
//	int tablerows[3] = { 11, 11, 14 }
	for(int i = 0; i < 14; ++i){
		table->setRowHidden(i, false);
	}

	QStringList lists[3];
	lists[0] << tr("TEMP(deg)") << tr("VALUE1") << tr("VALUE2") 
			 << tr("V2/V1") << tr("C_LOG") << tr("G_NUM") << tr("L_NUM")
			 << tr("RANGE1") << tr("GAIN1") << tr("RANGE2") << tr("GAIN2");
	lists[1] << tr("MAX.VL") << tr("MIN.VL") << tr("STP.VL") << tr("RANGE")
			 << tr("GAIN") << tr("VL1.AD") << tr("VL2.AD") << tr("CO.a1")
			 << tr("CO.a2") << tr("CO.b") << tr("CO.d");
	lists[2] << tr("Rs") << tr("Gs") << tr("Bs") << tr("CoefR") << tr("CoefG")
			 << tr("CoefB") << tr("Cal x") << tr("Cal y") << tr("RANGE1")
			 << tr("GAIN1") << tr("RANGE2") << tr("GAIN2") << tr("RANGE3")
			 << tr("GAIN3");

	table->setVerticalHeaderLabels(lists[type]);

	if(type != 2){
		for(int i = 11; i < 14; ++i){
			table->setRowHidden(i, true);
		}
	}
}


void CalbForm::showMasterResult()
{
//	MeasProg* pMeasProg = MeasProg::getInstance();
//	program_s* p = pMeasProg->getProgPtr();
	

	qDebug() << "showMasterReuslt slot !!!!!!!!!!!";
	btnSaveCalb->setEnabled(true);

	switch(calb_no){
	case 0:
		showHueResult();
		break;
	case 1:
		showVlResult();
		break;
	case 2:
		showDlmResult();
		break;
	default:
		break;
	}
}

void CalbForm::showHueResult()
{
//	MeasProg* pMeasProg = MeasProg::getInstance();
//	program_s* p = pMeasProg->getProgPtr();
	Ctrl* pCtrl = Ctrl::getInstance();
	program_s* p = pCtrl->getProgShmPtr();
	Hue_cfg* hue = &p->kinds[0].hue_cfg[chn_no-1];

//	qDebug() << "q9020 N_lum = " << hue->N_lum;

	QLabel* label;

	label = qobject_cast<QLabel*>(table->cellWidget(0, 0));
	label->setText(QString::number(hue->Mastertemp));
	label = qobject_cast<QLabel*>(table->cellWidget(1, 0));
	label->setText(QString::number(hue->v1));
	label = qobject_cast<QLabel*>(table->cellWidget(2, 0));
	label->setText(QString::number(hue->v2));
	label = qobject_cast<QLabel*>(table->cellWidget(3, 0));
//	label->setText(QString::number(hue->v2/hue->v1));
	label = qobject_cast<QLabel*>(table->cellWidget(4, 0));
	label->setText(QString::number(hue->C_log));
	label = qobject_cast<QLabel*>(table->cellWidget(5, 0));
	label->setText(QString::number(hue->G_lum));
	label = qobject_cast<QLabel*>(table->cellWidget(6, 0));
	label->setText(QString::number(hue->N_lum));
	label = qobject_cast<QLabel*>(table->cellWidget(7, 0));
//	label->setText(hue->Mastertemp);
	label = qobject_cast<QLabel*>(table->cellWidget(8, 0));
//	label->setText(hue->Mastertemp);
	label = qobject_cast<QLabel*>(table->cellWidget(9, 0));
//	label->setText(hue->Mastertemp);
	label = qobject_cast<QLabel*>(table->cellWidget(10, 0));
//	label->setText(hue->Mastertemp);
}


void CalbForm::showDlmResult()
{
//	lists[2] << tr("Rs") << tr("Gs") << tr("Bs") << tr("CoefR") << tr("CoefG")
//			 << tr("CoefB") << tr("Cal x") << tr("Cal y") << tr("RANGE1")
//			 << tr("GAIN1") << tr("RANGE2") << tr("GAIN2") << tr("RANGE3")
//			 << tr("GAIN3");
	
	Ctrl* pCtrl = Ctrl::getInstance();
	program_s* p = pCtrl->getProgShmPtr();
	Dlm_cfg* dlm = &p->kinds[0].dlm_cfg[chn_no-1];

	QLabel* label;
	label = qobject_cast<QLabel*>(table->cellWidget(3, 0));
	label->setText(QString::number(dlm->CoefR));
	label = qobject_cast<QLabel*>(table->cellWidget(4, 0));
	label->setText(QString::number(dlm->CoefG));
	label = qobject_cast<QLabel*>(table->cellWidget(5, 0));
	label->setText(QString::number(dlm->CoefB));
}


void CalbForm::showVlResult()
{
//	lists[1] << tr("MAX.VL") << tr("MIN.VL") << tr("STP.VL") << tr("RANGE")
//			 << tr("GAIN") << tr("VL1.AD") << tr("VL2.AD") << tr("CO.a1")
//			 << tr("CO.a2") << tr("CO.b") << tr("CO.d");
	Ctrl* pCtrl = Ctrl::getInstance();
	program_s* p = pCtrl->getProgShmPtr();
	Vl_cfg* vl = &p->kinds[0].vl_cfg[chn_no-1];

	QLabel* label;
	label = qobject_cast<QLabel*>(table->cellWidget(5, 0));
	label->setText(QString::number(vl->v1));
	label = qobject_cast<QLabel*>(table->cellWidget(6, 0));
	label->setText(QString::number(vl->v2));
	label = qobject_cast<QLabel*>(table->cellWidget(7, 0));
	label->setText(QString::number(vl->a1));
	label = qobject_cast<QLabel*>(table->cellWidget(8, 0));
	label->setText(QString::number(vl->a2));
	label = qobject_cast<QLabel*>(table->cellWidget(9, 0));
	label->setText(QString::number(vl->b));
	label = qobject_cast<QLabel*>(table->cellWidget(10, 0));
	label->setText(QString::number(vl->d));
}


void CalbForm::saveCalbToProg()
{
	Ctrl* p = Ctrl::getInstance();
	p->cpyProgramFromShm();

	// for DEBUG
	program_s* pr = p->getProgShmPtr();
	qDebug() << "hue_cfg ================" << pr->kinds[0].hue_cfg[0].active;
	qDebug() << "vl_cfg ================" << pr->kinds[0].vl_cfg[0].active;
	qDebug() << "dlm_cfg ================" << pr->kinds[0].dlm_cfg[0].active;
}
